package com.alan.wifishare.view.impl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alan.libapi.model.business.UserSession;
import com.alan.libapi.model.persistent.ModifyPwdType;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.presenter.UserLoginPS;
import com.alan.wifishare.utils.Md5Util;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVUserLogin;

/**
 * @className: UserLoginActivity
 * @describtion: UserLoginActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 12:40
 */
public class UserLoginActivity extends BaseActivity implements IVUserLogin, View.OnClickListener {

    private static UserLoginPS userLoginPS = null;
    private EditText userLoginAccountEt;
    private EditText userLoginPwdEt;
    private TextView userLoginRegisterTv;
    private TextView userLoginFindPwdTv;
    private Button userLoginSubmitBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_login_layout);
        userLoginPS = new UserLoginPS(this);
        initView();
    }

    @Override
    protected void initView() {
        // 初始化控件，在点击登录时调用loginPresenter.login()方法
        userLoginAccountEt = (EditText)findViewById(R.id.userLoginAccountEt);
        userLoginPwdEt = (EditText)findViewById(R.id.userLoginPwdEt);
        userLoginRegisterTv = (TextView)findViewById(R.id.userLoginRegisterTv);
        userLoginFindPwdTv = (TextView)findViewById(R.id.userLoginFindPwdTv);
        userLoginSubmitBtn = (Button)findViewById(R.id.userLoginSubmitBtn);

        userLoginRegisterTv.setOnClickListener(this);
        userLoginFindPwdTv.setOnClickListener(this);
        userLoginSubmitBtn.setOnClickListener(this);
    }

    @Override
    public String getUserAccount() {
        // 从输入控件获取用户账号
        return userLoginAccountEt.getText().toString();
    }

    @Override
    public String getUserPwd() {
        //从输入控件获取用户密码，需要对密码进行MD5加密
        return userLoginPwdEt.getText().toString();
    }

    @Override
    public void showProgress() {
        // 显示正在登录的进度框
        progressDialogShow();
    }

    @Override
    public void hideProgress() {
        // 隐藏正在登录的进度框
        progressDialogHiden();
    }

    @Override
    public void loginSuccess(UserSession userSession) {
        //登录成功进行跳转动作
        toastMsg("登录成功");
        String userPwd = Md5Util.stringToMd5(getUserPwd());
        GlobalField.userConfig.setUserAccount(getUserAccount());
        GlobalField.userConfig.setUserPwd(userPwd);
        GlobalField.userSession = userSession;
        if(GlobalField.userConfig.isFirstUse()){
            ActivityJumpUtil.jump2UserGuideAct(this);
            GlobalField.userConfig.setIsFirstUse(false);
        }else {
            ActivityJumpUtil.jump2MainAct(this);
        }
        this.finish();
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.userLoginRegisterTv:
                ActivityJumpUtil.jump2UserRegisterAct(this);
                this.finish();
                break;
            case R.id.userLoginFindPwdTv:
                ActivityJumpUtil.jump2VerifyIdentityFindPwdAct(this, ModifyPwdType.FOR_LOGIN_PWD);
                break;
            case R.id.userLoginSubmitBtn:
                userLoginPS.login();
                break;
            default:
                break;
        }
    }
}
